import cv2 as cv
import os

folder_path = './myphoto/'  # 文件夹路径

# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
    if filename.endswith('.jpg'):
        # 构造文件的完整路径
        file_path = os.path.join(folder_path, filename)

        # 读取图片
        img = cv.imread(file_path)

        # 将图片灰度转换
        gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

        # 构造保存的文件路径
        new_file_path = os.path.join('./data/',filename)

        # 保存图片
        cv.imwrite(new_file_path, gray_img)